package master

import (
	"io/ioutil"
	"encoding/json"
	"fmt"
)

//程序配置结构体
type Config struct {
	ApiPort         int `json:"apiPort"`
	ApiReadTimeout  int `json:"apiReadTimeout"`
	ApiWriteTimeout int `json:"apiWriteTimeout"`
	EtcdEndpoints []string `json:"etcdEndpoints"`
	EtcdDialTimeout int `json:"etcdDialTimeout"`
	WebRoot string `json:"webroot"`
	MongodbUri            string   `json:"mongodbUri"`
	MongodbConnectTimeout int      `json:"mongodbConnectTimeout"`
}

var (
	G_config *Config
)
//加载配置到全局结构体
func InitConfig(filename string) (err error) {
	var (
		content []byte
		conf    Config
	)
	//1.把配置文件读进来
	if content, err = ioutil.ReadFile(filename); err != nil {
		return
	}

	//2.json反序列化到Config结构体中
	if err = json.Unmarshal(content, &conf); err != nil {
		return
	}

	//3.赋值单例
	G_config = &conf
	fmt.Println("解析配置文件成功:",*G_config)

	return
}
